package com.example.springbootdemo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

// 实现的接口的V就是call()方法要返回的结果的类型。
public class ThreadCallable implements Callable<Integer> {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        System.out.println("主线程开始执行！");
        ThreadCallable threadCallable = new ThreadCallable();
        FutureTask<Integer> integerFutureTask = new FutureTask<>(threadCallable);
        new Thread(integerFutureTask).start();
        System.out.println("主线程继续执行1");
//        Integer result = integerFutureTask.get();
        System.out.println("主线程继续执行2");
//        System.out.println("子线程返回结果："+result);
        System.out.println("主线程执行结束！");
    }

    // Callable<V>接口的call()方法声明为：V call() throws Exception;
    // 实现类在重写时可以：
    // - 不抛出任何异常
    // - 抛出Exception本身
    // - 抛出Exception的任何子类异常
    // - 抛出运行时异常（RuntimeException及其子类）
    @Override
    public Integer call() throws InterruptedException {
        System.out.println("子线程开始执行！");
        System.out.println(Thread.currentThread().getName() + ",执行计算操作");
        Thread.sleep(3000);
        System.out.println("子线程执行结束！");
        return 1;
    }
}
